import {
  createSlice,
  PayloadAction,
  createAsyncThunk,
  ActionReducerMapBuilder
} from "@reduxjs/toolkit"
import axios from "axios"

interface ShoppingCarttate {
  loading: boolean
  error: string | null
  items: any[]
  // items1: [
  //   {
  //     touristRouteId: ""
  //     touristRoute: {
  //       id: ""
  //       title: ""
  //       description: ""
  //       price: ""
  //       originalPrice: ""
  //       discountPresent: ""
  //       rating: ""
  //       traveldays: ""
  //       tripType: ""
  //       departureCoty: ""
  //     }
  //     shoppingCartId: ""
  //     originalPrice: ""
  //     discountPresent: ""
  //   }
  // ]
}

const initialState: ShoppingCarttate = {
  loading: true,
  error: null,
  items: []
}

export const getShoppingCart = createAsyncThunk(
  "shoppingCart/getShoppingCart",
  async (jwt: string, thunkAPI) => {
    const { data } = await axios.get(
      `http://123.56.149.216:8080/api/shoppingCart`,
      {
        headers: {
          Authorization: `bearer ${jwt}`
        }
      }
    )
    return data.shoppingCartItems
  }
)

export const addShoppingCartItem = createAsyncThunk(
  "shoppingCart/addShoppingCartItem",
  async (parameters: { jwt: string; touristRouteId: string }, thunkAPI) => {
    const { data } = await axios.post(
      `http://123.56.149.216:8080/api/shoppingCart/items`,
      {
        touristRouteId: parameters.touristRouteId
      },
      {
        headers: {
          Authorization: `bearer ${parameters.jwt}`
        }
      }
    )
    return data.shoppingCartItems
  }
)

export const checkout = createAsyncThunk(
  "shoppingCart/checkout",
  async (jwt: string, thunkAPI) => {
    // const { data } = await axios.post(
    //   `http://123.56.149.216:8080/api/shoppingCart/checkout`,
    //   null,
    //   {
    //     headers: {
    //       Authorization: `bearer ${jwt}`
    //     }
    //   }
    // )
    // return data
  }
)

export const clearShoppingCartItem = createAsyncThunk(
  "shoppingCart/clearShoppingCartItem",
  async (parameters: { jwt: string; itemIds: number[] }, thunkAPI) => {
    return await axios.delete(
      `http://123.56.149.216:8080/api/shoppingCart/items/(${parameters.itemIds.join(
        ","
      )})`,
      {
        headers: {
          Authorization: `bearer ${parameters.jwt}`
        }
      }
    )
  }
)

export const shoppingCartSlice = createSlice({
  name: "shoppingCart",
  initialState,
  reducers: {},
  extraReducers: (builder: ActionReducerMapBuilder<ShoppingCarttate>) => {
    builder
      .addCase(getShoppingCart.pending, (state) => {
        // 处理pending状态，比如设置加载标志
        // return { ...state, loading: true };
        state.loading = true
      })
      .addCase(getShoppingCart.fulfilled, (state, action) => {
        // 处理fulfilled状态，使用action.payload更新state
        state.items = action.payload
        state.loading = false
        state.error = null
      })
      // .addCase(
      //   getShoppingCart.rejected,
      //   (state, action: PayloadAction<string | null>) => {
      //     // 处理rejected状态，比如设置错误信息
      //     state.loading = false
      //     // state.error = action.payload
      //   }
      // )
      .addCase(addShoppingCartItem.pending, (state) => {
        // 处理pending状态，比如设置加载标志
        // return { ...state, loading: true };
        state.loading = true
      })
      .addCase(addShoppingCartItem.fulfilled, (state, action) => {
        // 处理fulfilled状态，使用action.payload更新state
        state.items = action.payload
        state.loading = false
        state.error = null
      })
      // .addCase(
      //   addShoppingCartItem.rejected,
      //   (state, action: PayloadAction<string | null>) => {
      //     // 处理rejected状态，比如设置错误信息
      //     state.loading = false
      //     // state.error = action.payload
      //   }
      // )
      .addCase(clearShoppingCartItem.pending, (state) => {
        // 处理pending状态，比如设置加载标志
        // return { ...state, loading: true };
        state.loading = true
      })
      .addCase(clearShoppingCartItem.fulfilled, (state, action) => {
        // 处理fulfilled状态，使用action.payload更新state
        state.items = []
        state.loading = false
        state.error = null
      })
      // .addCase(
      //   clearShoppingCartItem.rejected,
      //   (state, action: PayloadAction<string | null>) => {
      //     // 处理rejected状态，比如设置错误信息
      //     state.loading = false
      //     // state.error = action.payload
      //   }
      // )
      .addCase(checkout.pending, (state) => {
        // 处理pending状态，比如设置加载标志
        // return { ...state, loading: true };
        state.loading = true
      })
      .addCase(checkout.fulfilled, (state, action) => {
        // 处理fulfilled状态，使用action.payload更新state
        state.items = []
        state.loading = false
        state.error = null
      })
    // .addCase(
    //   checkout.rejected,
    //   (state, action: PayloadAction<string | null>) => {
    //     // 处理rejected状态，比如设置错误信息
    //     state.loading = false
    //     // state.error = action.payload
    //   }
    // )
  }
})
